import unittest
from cvirt.cnode.libvirtXmlObject.networkFilter import *

class TestNetworkFilter(unittest.TestCase):
    def testCreateNetworkFilter(self):
        filter = NetworkFilter()
        filter.name = 'test-filter'
        filter.chain = 'ipv4'

        chain = FilterRules(action='accept',direction='in',priority='500',
                            ip_rules=[Rule(protocal='tcp',dstportstart='20',dstportend='40')])

        chain2 = FilterRules(action='drop',direction='out',priority='100',
                             ip_rules=[Rule(protocal='icmp')])


        filter.rules.append(chain)
        filter.rules.append(chain2)
        self.assertEqual(filter.to_xml(pretty_print=False),
                                        '<filter name="test-filter" chain="ipv4">'
                                          '<rule priority="500" action="accept" direction="in">'
                                            '<tcp dstportstart="20" dstportend="40"/>'
                                          '</rule>'
                                        '<rule priority="100" action="drop" direction="out">'
                                            '<icmp/>'
                                          '</rule>'
                                        '</filter>'
                                        )



    def testNetworkFilter_parse_xml(self):
        filter = NetworkFilter(parse_xml='''<filter name='test-eth0' chain='ipv4'>
                                          <uuid>7886f647-75ec-d104-0629-a55d23242d46</uuid>
                                          <filterref filter='clean-traffic'/>
                                          <filterref filter='allow-dhcp'/>
                                          <rule action='accept' direction='in' priority='500'>
                                            <tcp dstportstart='22'/>
                                          </rule>
                                          <rule action='accept' direction='in' priority='500'>
                                            <tcp dstportstart='8000'/>
                                          </rule>
                                          <rule action='accept' direction='out' priority='500'>
                                            <all/>
                                          </rule>
                                          <rule action='drop' direction='inout' priority='500'>
                                            <all/>
                                          </rule>
                                        </filter>''')

        self.assertEqual(filter.name,'test-eth0')
        self.assertEqual(filter.chain,'ipv4')
        self.assertEqual(filter.uuid,'7886f647-75ec-d104-0629-a55d23242d46')
        self.assertEqual(filter.refs,["clean-traffic","allow-dhcp"])

        self.assertNotEquals(filter.rules,[])
        rule = filter.rules[0]
        self.assertEqual(rule.action,'accept')
        self.assertEqual(rule.direction,'in')
        self.assertEqual(rule.priority,'500')
        self.assertNotEquals(rule.ip_rules,[])
        self.assertEqual(rule.ip_rules[0].protocal,'tcp')
        self.assertEqual(rule.ip_rules[0].dstportstart,'22')

        #no name, should raise exception.Error
        self.assertRaises(exception.Error,NetworkFilter,parse_xml='''<filter chain='ipv4'>
                                          <filterref filter='clean-traffic'/>
                                          <filterref filter='allow-dhcp'/>
                                        </filter>''')


    def test_modify_netfilter_from_xml(self):
        filter = NetworkFilter(parse_xml='''<filter name='test-eth0' chain='ipv4'>
                                          <uuid>7886f647-75ec-d104-0629-a55d23242d46</uuid>
                                        </filter>''')

        self.assertEqual(filter.rules,[])
        filter.name = 'test-aaa'
        filter.chain = 'root'
        filter.refs.append('allow-dhcp')

        rule = Rule(protocal = 'icmp',srcipaddr = "172.16.10.25")
        rules = FilterRules(action = 'accept',direction = 'inout',ip_rules=[rule])

        filter.rules.append(rules)

        self.assertEqual(filter.to_xml(pretty_print=False),'<filter name="test-aaa" chain="root">'
                                                               '<uuid>7886f647-75ec-d104-0629-a55d23242d46</uuid>'
                                                               '<filterref filter="allow-dhcp"/>'
                                                               '<rule priority="500" action="accept" direction="inout">'
                                                                    '<icmp srcipaddr="172.16.10.25"/>'
                                                               '</rule>'
                                                           '</filter>')
